<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
    :destroyOnClose='true'
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="id"
          :hidden="true"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
            :readonly="editable || showable"
          />
        </a-form-item>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="请输入权限名称"
                :readonly="showable"
                v-decorator="['name', {rules: [{required: true, message: '请输入权限名称'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                :readonly="showable"
                placeholder="请输入权限代码"
                v-decorator="['perms', {rules: [{required: true, message: '请输入权限代码'}]}]" />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="所属应用"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-select
                placeholder="请选择应用分类"
                :dicUrl="'/app/dropdown'"
                v-decorator="['application', {rules: [{required: true, message: '请选择应用分类'}],initialValue: code}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="菜单类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-radio-group
                placeholder="请选择菜单类型"
                :dicUrl="'/dict/type/menuType'"
                v-decorator="['type', {rules: [{required: true, message: '请选择菜单类型'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="父级菜单"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <biz-select-tree
                placeholder="请选择父级菜单"
                :dicUrl="`/perm/select?code=${code}`"
                v-decorator="['parentId',{rules: [{required: true, message: '请选择父级菜单'}]}]"
              >
              </biz-select-tree>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>

    <template slot="footer">
      <div v-show="!showable">
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addObj, putObj, getObj } from '@api/sys/perm'
import { FormMixin } from '@/mixins/FormMixin'
const isOrNoList = [
  {
    label: '是',
    value: '1'
  },
  {
    label: '否',
    value: '0'
  }
]
export default {
  name: 'PermissionAddOrUpdate',
  mixins: [FormMixin],
  props: {
    code: {
      type: String
    }
  },
  data () {
    return {
      isOrNoList,
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      treeData: [],
    }
  },
  methods: {
    edit (tmpRecord, type, permsType) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(tmpRecord.id).then(res => {
          const record = res.data
          record.parentId = String(record.parentId)
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'name', 'type', 'perms', 'parentId','application']))
          })
        })
      } else {
        resetFields()
      }
    },
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
            setTimeout(() => {
              this.confirmLoading = false
              this.$emit('ok')
              this.visible = false
            }, 1500)
          } catch (err) {
            this.$message.error(err.msg)
            this.confirmLoading = false
          }
        }
      })
    },
    // 选择图表弹出
    openIconSele () {
      this.visibleIcon = true
    },
    // 取消选择图表
    handleCancelIcon () {
      this.visibleIcon = false
    },
    // 选择图表
    handleIconChange (icon) {
      console.log('icon',icon)
      this.form.resetFields(`icon`,icon);
      this.form.getFieldDecorator('icon', { initialValue: icon })
      this.visibleIcon = false
    },
  }
}
</script>

<style scoped>

</style>
